package com.mnt.washio.activities;

import android.app.Dialog;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RatingBar;
import android.widget.RatingBar.OnRatingBarChangeListener;
import android.widget.Toast;

import com.mnt.washio.R;
import com.mnt.washio.objects.OrderObject;
import com.mnt.washio.utils.Statics;
import com.mnt.washio.utils.network.WebServiceCommunicatorListener;
import com.mnt.washio.utils.network.ws.BaseWSControl;
import com.mnt.washio.utils.network.ws.BaseWSControl.WebServiceFlag;
import com.mnt.washio.utils.network.ws.RateOrderWSControl;

public class OrderComplete extends BaseActivity implements WebServiceCommunicatorListener, View.OnClickListener {
	private OrderObject mOrderObject;
	private RatingBar mRtbOrder;
	private Button mBtnRate;
	private boolean isRated = false;
	private ProgressDialog pd;
	private Dialog myDialog;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_order_complete);

		initView();
		initProperties();
		initListeners();
	}

	@Override
	protected void initView() {

		mRtbOrder = (RatingBar) findViewById(R.id.rtb_rate);
		mRtbOrder.setStepSize(1.0f);
		
		mRtbOrder.setOnRatingBarChangeListener(new OnRatingBarChangeListener() {
			public void onRatingChanged(RatingBar ratingBar, float rating,
					boolean fromUser) {
				isRated = true;
			}
		});

		mBtnRate = (Button) findViewById(R.id.btn_rate);

	}

	private void initProperties() {
		mOrderObject = (OrderObject) getIntent().getSerializableExtra(
				"ORDER_OBJECT");
		pd = new ProgressDialog(this);
		pd.setCancelable(false);
		pd.setMessage("Saving...");
	}

	private void initListeners() {
		mBtnRate.setOnClickListener(this);
	}

	@Override
	public void onClick(View arg0) {
		switch (arg0.getId()) {
		case R.id.btn_rate:
			if (isRated) {
				int rate = (int) mRtbOrder.getRating();
				if(rate < 4){
					showDialogNote();
				}else{
					new RateOrderWSControl(this, this, mOrderObject.getOrderId(),
							rate, "").execute();
				}
			}
			break;

		default:
			break;
		}
	}

	public void showDialogNote() {
		myDialog = new Dialog(this);
		View view = LayoutInflater.from(this).inflate(
				R.layout.layout_note_ratting, null);
		final EditText mEdt = (EditText) view.findViewById(R.id.ed_email_forgot);
		Button mBtn = (Button) view.findViewById(R.id.btn_send);
		myDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
		myDialog.setContentView(view);
		Window window = myDialog.getWindow();
		WindowManager.LayoutParams wlp = window.getAttributes();
		wlp.gravity = Gravity.CENTER;
		wlp.width = WindowManager.LayoutParams.MATCH_PARENT;
		wlp.flags &= WindowManager.LayoutParams.FLAG_DIM_BEHIND;
		window.setAttributes(wlp);
		myDialog.getWindow().setAttributes(wlp);
		myDialog.show();
		
		mBtn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				String note = mEdt.getText().toString().trim();
				if(note.length() == 0){
					Statics.showToast(getBaseContext(), "Please input what do you think!");
				}else{
					new RateOrderWSControl(OrderComplete.this, OrderComplete.this, mOrderObject.getOrderId(),
							(int) mRtbOrder.getRating(), note).execute();
					myDialog.dismiss();
				}
			}
		});
	}
	
	@Override
	public void onConnectionOpen(BaseWSControl wsControl, WebServiceFlag flag) {
		runOnUiThread(new Runnable() {

			@Override
			public void run() {
				if(null != pd)
					pd.show();
			}
		});
	}

	@Override
	public void onConnectionError(BaseWSControl wsControl, WebServiceFlag flag,
			String error) {
		pd.dismiss();
	}

	@Override
	public void onConnectionDone(BaseWSControl wsControl, WebServiceFlag flag,
			Object result) {
		pd.dismiss();
		finish();
	}

	@Override
	public void onUpdateData(BaseWSControl wsControl, WebServiceFlag flag,
			Object data) {
		// TODO Auto-generated method stub
		
	}
}
